import React from 'react';
import {Spin, Card, Button, Table, Row, Col, Checkbox} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {currency} from '../Enum';
import routes from './routes';
import {TABLE} from '../constants';
import {conventEnumValueToString, formatAmount} from '../utils';
import {getDetail} from './api';
import {injectIntl} from './intl';
const {Description} = DescriptionList;
class DetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    render() {
        const {data} = this.state;
        const {formatMessage} = this.props.intl;
        const colums = [{
            title: formatMessage({
                id: 'detailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.quantity',
                defaultMessage: '备件数量'
            }),
            dataIndex: 'quantity'
        }, {
            title: formatMessage({
                id: 'detailPanel.column.cif',
                defaultMessage: '运保费'
            }),
            dataIndex: 'cif',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.discountFee',
                defaultMessage: '折扣金额'
            }),
            dataIndex: 'discountFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.bonusFee',
                defaultMessage: '奖励金额'
            }),
            dataIndex: 'bonusFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.claimFee',
                defaultMessage: '索赔金额'
            }),
            dataIndex: 'claimFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.declarationDiffFee',
                defaultMessage: '报关差异金额'
            }),
            dataIndex: 'declarationDiffFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.otherFee',
                defaultMessage: '其它金额'
            }),
            dataIndex: 'otherFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.exchangeDiffFee',
                defaultMessage: '季度汇率差异'
            }),
            dataIndex: 'exchangeDiffFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.rebateFee',
                defaultMessage: '奇瑞返利兑现'
            }),
            dataIndex: 'rebateFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.cost',
                defaultMessage: '备件成本价'
            }),
            dataIndex: 'cost',
            render: text => formatAmount(text)
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailPanel.card.common',
                                defaultMessage: '基本信息'
                            })
                        }</Col></Row>}>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.sapOrderCode',
                                defaultMessage: 'SAP销售订单号'
                            })}>{data.sapOrderCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.sapDeliveryCode',
                                defaultMessage: 'SAP发货单号'
                            })}>{data.sapDeliveryCode}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.currency',
                                defaultMessage: 'SAP销售单币种'
                            })}>{conventEnumValueToString(currency, data.currency)}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.cost',
                                defaultMessage: 'SAP销售单金额'
                            })}>{formatAmount(data.cost)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.invoiceCode',
                                defaultMessage: 'DMS发票号'
                            })}>{data.invoiceCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.invoiceAmount',
                                defaultMessage: '发票金额'
                            })}>{formatAmount(data.invoiceAmount)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.invoiceCurrency',
                                defaultMessage: '发票币种'
                            })}>{conventEnumValueToString(currency, data.invoiceCurrency)}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'detailPanel.table.details',
                            defaultMessage: '清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={colums}
                                rowKey="partId"
                                pagination={false}
                                dataSource={this.state.details}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};

import {connect} from 'react-redux';
const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(null, mapDispatchToProps)(injectIntl(DetailPanel));
